<?php

namespace App\Trip\Passengers;

use Exception;
use JetBrains\PhpStorm\ArrayShape;

class OrderSubPassenger
{


    public static function buildSubPassengerId(string $passenger_id, int $sequence): string
    {
        return "{$passenger_id}_{$sequence}";
    }

    /**
     * @param string $passenger_id
     *
     * @return array
     * @throws Exception
     */
    #[ArrayShape([
        "passenger_id" => "",
        "sequence"     => "",
    ])]
    public static function parsePassengerId(string $passenger_id): array
    {
        $explode = explode('_', $passenger_id);
        if (empty($explode) || count($explode) > 2) {
            throw new Exception("订单乘客id格式错误");
        }
        if (count($explode) == 1) {
            return [
                "passenger_id" => $explode[0],
                "sequence"     => "",
            ];
        }
        return [
            "passenger_id" => $explode[0],
            "sequence"     => $explode[1],
        ];
    }
}